#! /bin/bash
# My first script

## set dte variable to the date from which u want to record weather data
dte="2012/09/10"
echo $dte
rm WeatherData.txt
## run for loop up to number of days for which u want data
for i in {1..10}
do
    vardate=`date +"%Y/%m/%d" --date "$dte $i days"`
    echo $vardate
    filename=`date +"%d_%m_%Y" --date "$dte $i days"`
    wget -O $filename.html "http://www.wunderground.com/history/airport/VABB/$vardate/DailyHistory.html?req_city=NA&req_state=NA&req_statename=NA&format=1"
    #filter the file for </br> tag
    cat $filename.html |sed 's:<br />::g' >$filename.txt
    #delete the html file
    rm $filename.html
    
    #delete top 3 line with header
    sed '1,3d' $filename.txt > newfile.txt
    rm $filename.txt
    mv newfile.txt $filename.txt
    
    ##convert 12 hr time into 24 hour format
    ##
    # copy first column form $filename.txt , it is time in 12 hr format
    cat $filename.txt |awk 'BEGIN { FS = "," } ; { print $1 }' >1.txt
    
    while read LINE
    do
	#2.txt contains time in 24 hr format
	var=`date --date="$LINE" +%T`
	echo "$filename $var" >>2.txt
    done < 1.txt
    #copy 2nd to 14th column in 1.txt
    cat $filename.txt |awk 'BEGIN { FS = "," } ; { print $2","$3","$4","$5","$6","$7","$8","$9","$10","$11","$12","$13","$14 }' >1.txt
    #join to files verivally
    paste 2.txt 1.txt >3.txt
    #filter noise in file
    #rm WeatherData.txt
    sed 's:\t:,:g'<3.txt>>WeatherData.txt
    
    #delete unused files
    rm 1.txt 2.txt 3.txt $filename.txt
done

#------------------------------------------------------------
date2stamp () {
    date --utc --date "$1" +%s
}
dateDiff (){
    case $1 in
        -s)   sec=1;      shift;;
        -m)   sec=60;     shift;;
        -h)   sec=3600;   shift;;
        -d)   sec=86400;  shift;;
        *)    sec=86400;;
    esac
    dte1=$(date2stamp $1)
    dte2=$(date2stamp $2)
    diffSec=$((dte2-dte1))
    if ((diffSec < 0)); then abs=-1; else abs=1; fi
    echo $((diffSec/sec*abs))
}
#------------------------------------------------------------
rm 1.txt
while read LINE
do
       #cut the following line for time
       #25_08_2012 00:40:00,28.0,25.0,84,1008,4.0,Calm,Calm,-,N/A,,Haze,0,2012-08-23 19:10:00
    var=`echo $LINE |cut -c 12-19`
    #now only filter time stamp with 40 and 10 minutes recorded for simplicity
    varmin=`echo $var |cut -c 4-5`
    if [ "$varmin" = "40" ]
    then
	#echo $varmin
	##interpolate same time 10 minutes prior
	before=`date --date "$var 10 minutes ago"`
	before10=`echo $before| cut -c 12-19`
	echo `echo $LINE |cut -c -11`$before10`echo $LINE |cut -c 20-` >>1.txt
	#echo $before10 >>1.txt
	##interpolate same time 5  minutes later
	later=`date --date "$var 5 minutes"`
	later5=`echo $later| cut -c 12-19`
	#echo $later5 >>1.txt
	echo `echo $LINE |cut -c -11`$later5`echo $LINE |cut -c 20-`>>1.txt
    elif [ "$varmin" = "10" ]
    then
	#echo $varmin
	##interpolate same time 10 minutes prior
	before=`date --date "$var 10 minutes ago"`
	before10=`echo $before| cut -c 12-19`
	echo `echo $LINE |cut -c -11`$before10`echo $LINE |cut -c 20-` >>1.txt
	#echo $before10 >>1.txt
	##interpolate same time 5  minutes later
	later=`date --date "$var 5 minutes"`
	later5=`echo $later| cut -c 12-19`
	#echo $later5 >>1.txt
	echo `echo $LINE |cut -c -11`$later5`echo $LINE |cut -c 20-`>>1.txt
    fi
done < WeatherData.txt

rm 2.txt
start="00:00:00"
while read LINE
do

    line=`echo $LINE| cut -c 12-19`
    #echo $line
    difference=`dateDiff -m $start $line`
    let "counter=($difference/15)+1"
      #counter=`echo $counter |bc`
      #for i in { 1..$counter }
    for (( i = 0 ; i < $counter ; i++ ));  do
    	#echo $i "one"
	echo `echo $LINE |cut -c -11`$start`echo $LINE |cut -c 20-` >>2.txt
    	#echo $start $line
    	plus15=`date --date "$start 15 minutes"`
    	start=` echo $plus15 |cut -c 12-19`
    done
done <1.txt

rm  WeatherData.txt
rm 1.txt
mv 2.txt  WeatherData.txt
